package apps.sunmin.grave.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;


public class HttpUtil {

	public int TIME_OUT = 5 * 1000;
	
	URL url;
	HttpURLConnection http;
	PrintWriter out ;
	BufferedReader in;
	
	public HttpUtil()
	{}
	
	
	public HttpUtil( int timeOut )
	{
		TIME_OUT = timeOut * 1000;
	}
	
	
	
	public String requestGet ( String page , HashMap <String, String >map) throws Exception
	{
		//AppUtil.debug( this , "URL GET " + page );
		
	
		
		String send = null;
		
		StringBuffer sb = new StringBuffer("");
		
		if( map != null )
		{
			Iterator<String> iter = map.keySet().iterator();
		    
			String key , val ;
			
	        while(iter.hasNext())
	        {
	            key    = iter.next();
	            val    =  map.get(key);
	            
	            sb.append( key + "=" + URLEncoder.encode(val , "UTF-8") + "&");
	        }

	        if( sb != null && sb.toString().length() > 1 )
	        {
	        	send = sb.toString();
	        	send = send.substring(0 , send.length() - 1 );
	        }	
		}	
		
		//AppUtil.debug( "SEND URL["+ page + "?" + send + "]");
		
		
		url = new URL( page + "?" + send ); 
		
		http = (HttpURLConnection)url.openConnection();
		http.setConnectTimeout(TIME_OUT);
		http.setDefaultUseCaches(false);
	//	http.setDoInput(true);
		http.setDoOutput(true);
		http.setRequestMethod("GET");
		http.setRequestProperty("content-type", "application/x-www-form-urlencoded");
		
		
		if( send != null )
		{
			out = new PrintWriter( new OutputStreamWriter( http.getOutputStream() ) );
			out.write(send);
			out.flush();
			out.close();
		}
		
	
		sb = new StringBuffer();
		in = new BufferedReader ( new InputStreamReader ( http.getInputStream() , "UTF-8"));
		
		String line;
		
		while( (line = in.readLine()) != null )
		{
			sb.append(line);
		}
		
		//AppUtil.debug( "RECV[" + sb.toString() + "]");
		
		return sb.toString();
	}
	
	public String requestPost ( String page , HashMap <String, String >map) throws Exception
	{
		
		//AppUtil.debug( this , "URL POST " + page );
		
		url = new URL( page); 
		
		http = (HttpURLConnection)url.openConnection();
		http.setConnectTimeout(TIME_OUT);
		http.setDefaultUseCaches(false);
	//	http.setDoInput(true);
		http.setDoOutput(true);
		http.setRequestMethod("POST");
		http.setRequestProperty("content-type", "application/x-www-form-urlencoded");
		
		
		String send = null;
		
		StringBuffer sb = new StringBuffer("");
		
		if( map != null )
		{
			Iterator<String> iter = map.keySet().iterator();
		    
			String key , val ;
			
	        while(iter.hasNext())
	        {
	            key    = iter.next();
	            val    =  map.get(key);
	            
	            sb.append( key + "=" + URLEncoder.encode(val , "UTF-8") + "&");
	        }

	        if( sb != null && sb.toString().length() > 1 )
	        {
	        	send = sb.toString();
	        	send = send.substring(0 , send.length() - 1 );
	        }	
		}	
		
		//AppUtil.debug( "SEND URL[" + send + "]");
		
		if( send != null )
		{
			out = new PrintWriter( new OutputStreamWriter( http.getOutputStream() ) );
			out.write(send);
			out.flush();
			out.close();
		}
		
	
		sb = new StringBuffer();
		in = new BufferedReader ( new InputStreamReader ( http.getInputStream() , "UTF-8"));
		
		String line;
		
		while( (line = in.readLine()) != null )
		{
			sb.append(line);
		}
		
		//AppUtil.debug( "RECV[" + sb.toString() + "]");
		
		return sb.toString();
	}
	
	public void close() 
	{
		try{if( out != null ) out.close();}catch(Exception ig){}
		
		try{if( in != null ) in.close();}catch(Exception ig){}
		
		try{if( http != null ) http.disconnect();}catch(Exception ig){}
	
		out= null;
		in = null;
		http = null;
	}
}
